import Vue from 'vue'
import VueRouter from 'vue-router'

const Home = () => import('@/views/home/Home')

const Profile = () => import('@/views/profile/Proflie')
const UserInfoPage = () => import('@/views/profile/UserInfoPage')
const UserOrderList = () => import('@/views/profile/UserOrderList')
const UserComment = () => import('@/views/profile/UserComment')
const UserFavorite = () => import('@/views/profile/UserFavorite')
const UserAddress = () => import('@/views/profile/UserAddress')

const Item = () => import('@/views/item/Item')

const Cart = () => import('@/views/cart/Cart')

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    component: Home,
    meta: {
      title: '欢迎来到LCZW商城~'
    }
  },
  {
    path: '/user',
    component: Profile,
    children: [
      {
        path: '/',
        redirect: 'portal'
      },
      {
        path: 'portal',
        component: UserInfoPage,
        meta: {
          title: '个人中心'
        },
      },
      {
        path: 'orderList',
        component: UserOrderList,
        meta: {
          title: '我的订单'
        },
      },
      {
        path: 'comment',
        component: UserComment,
        meta: {
          title: '评价晒单'
        },
      },
      {
        path: 'favorite',
        component: UserFavorite,
        meta: {
          title: '我的收藏'
        },
      },
      {
        path: 'address',
        component: UserAddress,
        meta: {
          title: '收货地址'
        },
      },
    ]
  },
  {
    path: '/item',
    component: Item,
    meta: {
      title: ''
    }
  },
  {
    path: '/cart',
    name: 'Cart',
    component: Cart,
    meta: {
      title: '我的购物车'
    }
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  //可以在前置守卫这里判断用户有没有登录之类的操作 详情看https://router.vuejs.org/zh/guide/advanced/navigation-guards.htm或者https://www.bilibili.com/video/BV15741177Eh?p=116 第10分钟
  //从from跳转到to
  document.title = to.meta.title //代替在每个组件写created生命周期来修改页面标题
  // console.log(to);
  // console.log('前置守卫guard');
  next()
})

export default router
